/*
 * Copyright © YOLANDA. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hks360.library.nohttp;

import android.content.Context;
import android.util.Log;

import com.hks360.des2.Des2;
import com.hks360.nohttp.NoHttp;
import com.hks360.nohttp.Request;
import com.hks360.nohttp.RequestMethod;
import com.hks360.nohttp.RequestQueue;
import com.hks360.nohttp.download.DownloadQueue;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created in Oct 23, 2015 1:00:56 PM
 *
 * @author YOLANDA
 */
public class CallServer {
	private static final String DATA = "data";

    private static CallServer callServer;

    /**
     * 请求队列
     */
    private RequestQueue requestQueue;

    /**
     * 下载队列
     */
    private static DownloadQueue downloadQueue;

    private CallServer() {
        requestQueue = NoHttp.newRequestQueue();
    }

    /**
     * 请求队列
     */
    public synchronized static CallServer getRequestInstance() {
        if (callServer == null)
            callServer = new CallServer();
        return callServer;
    }

    /**
     * 下载队列
     */
    public static DownloadQueue getDownloadInstance() {
        if (downloadQueue == null)
            downloadQueue = NoHttp.newDownloadQueue();
        return downloadQueue;
    }

    /**
     *  添加一个请求到请求队列
     * @param context       context对象
     * @param what 		 用来标志请求, 当多个请求使用同一个{@link HttpCallback}时, 在回调方法中会返回这个what
     * @param request	     请求对象
     * @param callback	     结果回调对象
     */
    public <T> void add(Context context,int what,Request<T> request,HttpCallback<T> callback) {
        requestQueue.add(what, request, new HttpResponseListener<T>(context, callback));
    }

    /**
     * 普通的GET请求
     * @param url		 请求的路径
     * @param what		 用于标志请求
     * @param callback	 结果回调对象
     */
    public void get(Context context,String url,int what,HttpCallback<String> callback) {
        Request<String> request = NoHttp.createStringRequest(url, RequestMethod.GET);
        add(context, what, request, callback);
    }
    
    /**
     * 普通的POST请求
     * @param url		 请求的路径
     * @param what		 用于标志请求
     * @param callback	 结果回调对象
     */
    public void post(Context context,String url,int what,String jsonStr,HttpCallback<String> callback) {
    	Request<String> request = NoHttp.createStringRequest(url,RequestMethod.POST);
        try {
            if(url.contains("UserSign")){
                Log.e("RWQ",jsonStr.toString());
                request.add(DATA,jsonStr);
            }else {
                Log.e("RWQ",jsonStr.toString());
                request.add(DATA, URLEncoder.encode(new String(Des2.encrypt(jsonStr)), "UTF-8"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        add(context, what, request, callback);
    }
    /**
     * 取消这个sign标记的所有请求
     */
    public void cancelBySign(Object sign) {
        requestQueue.cancelBySign(sign);
    }

    /**
     * 取消队列中所有请求
     */
    public void cancelAll() {
        requestQueue.cancelAll();
    }

    /**
     * 退出app时停止所有请求
     */
    public void stopAll() {
        requestQueue.stop();
    }

}
